-- 地下城经验卡

local ActiveReward = class("ActiveReward")

function ActiveReward:ctor()
	self.type = 0
	self.point = 0

	self.gainInfos = {}

	self._realValidPoint = 0 -- 实际有效的奖励点数
	self._eachLevelActiveStatus = {} -- 每一档奖励的激活状态
	self._eachLevelCumulationInfo = {} -- 每一档奖励的累积次数
end

function ActiveReward:read(value)
	self.type = 2 -- 此处只会是 地城经验卡
	self.point = value.point
	self.gainInfos = {}
	if value.expCards then
		for _,v in ipairs( value.expCards ) do

			table.insert( self.gainInfos, { level = v.level, cumulationNum = v.cardNum, expInfo = v.expInfo} )
		end
	end

	self:_calRealValidPoint()
	self:_parseEachLevelCumulationInfo()
end

-- 获取实际有效的点数
function ActiveReward:getRealValidPoint()
	self:_calRealValidPoint()
	return self._realValidPoint
end
-- 帮助函数: 根据 pointClock 计算实际有效的 point
function ActiveReward:_calRealValidPoint()
	self._realValidPoint = self.point
	self:_parseEachLevelActiveStatus()
end

-- 每一档奖励的激活状态
function ActiveReward:_parseEachLevelActiveStatus()
	for i = 1, 3 do
		if i <= self._realValidPoint then -- 激活
			self._eachLevelActiveStatus[ i ] = true
		else
			self._eachLevelActiveStatus[ i ] = false
		end
	end
end
-- 根据档位获取奖励的激活状态
function ActiveReward:getActiveStatusByLevel( level )
	return self._eachLevelActiveStatus[ tonumber( level ) ]
end

-- 每一档奖励的累积次数
function ActiveReward:_parseEachLevelCumulationInfo()
	for i = 1, 3 do
		local cumulationTimes = nil
		for _,v2 in ipairs( self.gainInfos ) do -- 服务器下来的数据
			if v2.level == i then
				cumulationTimes = v2.cumulationNum
			end
		end
		if not cumulationTimes then -- 服务器上没记录 v.level 这一档奖励领取的记录。服务器认为累积次数为1
			cumulationTimes = 1
		end

		self._eachLevelCumulationInfo[ i ] = cumulationTimes
	end
end
-- 根据档位获取奖励的累积次数
function ActiveReward:getCumulationInfoByLevel( level )
	return self._eachLevelCumulationInfo[ tonumber( level ) ]
end

-- 获取所有可以领取的奖励个数和
function ActiveReward:getAllCanReceivedCount()
	local count = 0
	for k,v in pairs( self._eachLevelActiveStatus ) do
		if v then
			count = count + self._eachLevelCumulationInfo[ tonumber( k ) ]
		end
	end
	return  count
end

-- 获取今日可领取奖励的最大个数（包括未激活的）
function ActiveReward:getAllCount()
	local count = 0
	for k,v in pairs( self._eachLevelCumulationInfo ) do
		count = count + v
	end
	return  count
end

return ActiveReward
